﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using bmapi.SQLServerAccess;

namespace bmapi.Controllers
{
    [Route("order"),JwtAuthActionFilter]
    public class OrderController : BuyiController
    {

        // PUT: api/Order/5
        /// <summary>
        /// 改变已有订单号的状态  并删除文件夹
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public HttpResponseMessage Put(dynamic obj)
        {
            SQLCommand sc = null;
            if (obj == null)
                return AssistFun.toJson(new { status = 201, tip = "参数不能为空" });
            else
            {
                int id = Convert.ToInt32(obj.Id);
                string ordername = obj.OrderId.ToString();
                try
                {
                    string sql = "update Order_tb set state=0 where id=" + id;
                    sc = new SQLCommand(sql);
                    int res = sc.ExecuteNonQuery();
                    if (res == 1)
                    {
                        string path = GetPCPath(ordername);
                        DeleteFolder(path);
                    }
                    return AssistFun.toJson(new { status = 200, tip = "成功" }); ;
                }
                catch (Exception e)
                {
                    sc = new SQLCommand("update Order_tb set state=1 where id=" + id);
                    sc.ExecuteNonQuery();
                    WriteDebug("ChangeOrderStateDelDir", e.Message);
                    return AssistFun.toJson(new { status = 201, tip = "失败" }); ;
                }
            }
        }
    }
}
